
/**
 * 财务管理控制层
 */
define([ "app", "jquery", "md5", "ecb", "artDialog","iframeTools",
        "services/UserService",
		"resources/UserResource", 
		"directives/showtimes/AddUser",
		"resources/AddYokiUserResource",
		"resources/ShowTimesUserResource"], function(app) {

	var deps = [ "$scope", "$state", "$modal","$location", "$stateParams","UserService","UserResource","AddYokiUserResource","ShowTimesUserResource" ];

	function controller($scope, $state, $modal,$location, $stateParams,UserService,UserResource,AddYokiUserResource,ShowTimesUserResource) {
		$scope.currentPage = 1;
        $scope.pages = [];
        $scope.pageSize = 10;
        //$scope.selected=10;
        queryData();
		
		function queryData(){
			ShowTimesUserResource.queryUserList($scope.currentPage,$scope.pageSize,$scope.keyWord).success(function(data){
				$scope.data = data.obj.userList;
				$scope.onlineCount=data.obj.onlineCount;
				if(data.obj.count % $scope.pageSize == 0){
         			$scope.totalPage=Math.floor(data.obj.count/$scope.pageSize);
        		}else{
        			$scope.totalPage=Math.floor(data.obj.count/$scope.pageSize)+1;
        		}
            });
		}
		
		$scope.$on('refeshUser', function($event, data) {
			$scope.keyWord="";
			queryData();
        });
		$scope.gotoPage = function (currentNum) {
			
			if($scope.currentPage != currentNum){
				 $scope.currentPage = currentNum;
		            queryData();
			}
        };
        
        $scope.nextPage = function () {
        	if($scope.currentPage<$scope.totalPage){
        		$scope.currentPage++;
        		queryData();
        	}
        };

        $scope.upPage = function () {
        	if($scope.currentPage>1){
        		$scope.currentPage--;
        		queryData();
        	}	
        };
        
        
        
        $scope.updateUser=function updateUser(userId){
        	
        	$state.go('home.updateUser', {
        		userId: userId
            });
		};
		
		
        $scope.addUserNew=function addUserNew(){
        	$state.go('home.addUserNew');
		};
		$scope.addUser=function addUser($choseUser){
			$scope.data=null;
			queryData();
		};
		
		 $scope.changePageSize =function(){
	          	//var showPageSize =$("#showPageSize").val();
	          	$scope.currentPage =1;
	          	//$scope.pageSize =showPageSize;
	          	ShowTimesUserResource.queryUserList($scope.currentPage,$scope.pageSize,$scope.keyWord).success(function(data){
					$scope.data = data.obj.userList;
					if(data.obj.count % $scope.pageSize == 0){
	         			$scope.totalPage=Math.floor(data.obj.count/$scope.pageSize);
	        		}else{
	        			$scope.totalPage=Math.floor(data.obj.count/$scope.pageSize)+1;
	        		}
	            });
	     };
	     
	     
	     $scope.updateStatus =function (userId ,status){
	    	 ShowTimesUserResource.updateStatus(userId ,status).success(function(data){
	    		 queryData();
	         });
	    	 
	     };
	     
	     
	     $scope.search=function (){
	    	 var keyWord=$scope.keyWord;
	    	 ShowTimesUserResource.queryUserList(0,$scope.pageSize,keyWord).success(function(data){
					$scope.data = data.obj.userList;
					$scope.currentPage =1;
					if(data.obj.count % $scope.pageSize == 0){
	         			$scope.totalPage=Math.floor(data.obj.count/$scope.pageSize);
	        		}else{
	        			$scope.totalPage=Math.floor(data.obj.count/$scope.pageSize)+1;
	        		}
	            });
	    	 
	     };
	     
	     $scope.deleteUser =function (userId){
	    	 ShowTimesUserResource.deleteUser(userId).success(function(data){
	    		 if(data.resCode!='0000'){
	    			 art.dialog.alert("删除失败");
	    		 }
	    		 else{
	    			 queryData();
	    		 }
	         });
	     };
		
	}
	controller.$inject = deps;
	return app.lazy.controller("UserManagementController", controller);
});
